﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common/AppDependencyResolver 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       AppDependencyResolver
* 创建时间：  2025/5/22 15:57:23
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common
{
    public class AppDependencyResolver
    {
        private static AppDependencyResolver _resolver;

        public static AppDependencyResolver Current
        {
            get
            {
                if (_resolver == null)
                    throw ServiceResult.Exception("AppDependencyResolver未初始化. 必须在Startup设置");
                return _resolver;
            }
        }

        public static void Init(IServiceProvider services)
        {
            _resolver = new AppDependencyResolver(services);
        }

        public static void Init(IContainer container)
        {
            _resolver = new AppDependencyResolver(container);
        }

        public IServiceProvider ServiceProvider { get; private set; }
        public IContainer ServiceContainer { get; private set; }

        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetService<T>()
        {
            if (ServiceProvider != null)
                return ServiceProvider.GetService<T>();
            else
                return ServiceContainer.Resolve<T>();
        }

        public T GetRequiredService<T>()
        {
            if (ServiceProvider != null)
                return ServiceProvider.GetRequiredService<T>();
            else
                return ServiceContainer.Resolve<T>();
        }

        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetScopeService<T>()
        {
            if (ServiceProvider != null)
                return ServiceProvider.CreateScope().ServiceProvider.GetService<T>();
            else
                return ServiceContainer.BeginLifetimeScope().Resolve<T>();
        }

        /// <summary>
        /// 获取新的Scope
        /// </summary>
        /// <returns></returns>
        public ILifetimeScope GetScope()
        {
            if (ServiceProvider != null)
            {
                return ServiceProvider.GetAutofacRoot().BeginLifetimeScope();
            }
            return ServiceContainer.BeginLifetimeScope();
        }

        private AppDependencyResolver(IServiceProvider serviceProvider)
        {
            ServiceProvider = serviceProvider;
        }

        private AppDependencyResolver(IContainer serviceContainer)
        {
            ServiceContainer = serviceContainer;
        }
    }
}
